package com.github.util;

import com.alibaba.fastjson.JSONObject;
import com.github.UiApplication;
import com.github.drinkjava2.jsqlbox.SqlBoxContext;
import org.h2.jdbcx.JdbcConnectionPool;

import java.util.Date;
import java.util.List;
import java.util.Map;

import static com.github.drinkjava2.jsqlbox.JSQLBOX.nExecute;
import static com.github.drinkjava2.jsqlbox.JSQLBOX.nQueryForMapList;

public class DataBaseInitUtil {
    private static JdbcConnectionPool jdbcCP = null;

    private static void DBConnectionInit() {
        String dbPath = "./config/data";
        jdbcCP = JdbcConnectionPool.create("jdbc:h2:" + dbPath, "sa", "");
        SqlBoxContext sqlBoxContext = new SqlBoxContext(jdbcCP);
        SqlBoxContext.setGlobalSqlBoxContext(sqlBoxContext);
    }

    public static void init() {
        DBConnectionInit();
        try {
            //nExecute("delete from  downloadHistory");
            List<Map<String, Object>> rs = nQueryForMapList("select * from downloadHistory");
            System.out.println(rs.size());
            for (Map<String, Object> m:rs
                 ) {
                System.out.println(JSONObject.toJSONString(m));
               // UiApplication.log.setSelectionStart(UiApplication.log.getText().length());
            }
        } catch (Exception e) {
            System.out.println("创建数据库");
            nExecute("create table downloadHistory(id varchar,artifactId varchar,groupId varchar,version varchar,state int,createTime TIMESTAMP,finshTime TIMESTAMP)");
        }

    }
}
